home *** CD-ROM | disk | FTP | other *** search
- /* fdio.c file - I/O module for FD program */
- #include "stdio.h"
- #include "cminor.h"
- #include "fdparm.h"
-
- /* fseek mode definitions */
- #define CURRENT_REL 1 /* relative to current position */
- #define EOF_REL 2 /* relative to end of the file */
- #define BOF_REL 0 /* relative to beginning of file*/
- #define CTL_Z 26 /* marks EOF in some text files */
-
- FILE *fp ; /* file pointer for text file */
- long filesize ; /* size of the file in bytes */
- long int ftell() ;
- FILE *gfopen() ;
-
- int open_file(fn) /* open a file */
- char fn[] ; /* file name string */
- { /* return sucess or failure */
- fp = gfopen(fn,"r",BIN_MODE) ;
- if(fp != NULL)
- return(SUCCESS) ;
- else return(FAILURE) ;
- }
-
- int set_filesize(chk_ctlz) /* set up filesize */
- int chk_ctlz ; /* if = ASC_MODE check for CONT-Z*/
- {
- long pos ;
- int c ;
-
- fseek(fp,0L,EOF_REL) ; /* get size of file */
- filesize = ftell(fp) ;
-
- if( chk_ctlz != ASC_MODE ) /* look for control Z */
- return ;
-
- /* check last 128 byte block for CTL-Z */
- pos = filesize - (filesize % 128) ;
- fseek(fp,pos,BOF_REL) ;
- c = getc(fp) ;
- while( c != EOF )
- { if( c == CTL_Z ) /* look for control-Z */
- { filesize = ftell(fp) - 1 ;
- return ; /* found - adjust file size & exit */
- }
- c = getc(fp) ;
- }
- }
-
-
- int move_to(new_pos) /* move to a specified position */
- long new_pos ;
- {
- fseek(fp, new_pos ,BOF_REL) ;
- }
-
- long where_now() /* return current position */
- {
- return(ftell(fp) ) ;
- }
-
- int get_next_char() /* get char at file position */
- {
- char c ;
-
- if( ftell(fp) == filesize ) /* are we at end of file ? */
- return( EOF_MARK ) ; /* yes - return eof mark */
- else /* no - get next char */
- { c = getc(fp) ; /* get a character */
- return( toascii(c) ) ; /* force char to ASCII, return it */
- }
- }
-
-
- int get_previous_char() /* read the char in front of */
- { /* current file position */
- char c ;
-
- if( ftell(fp) != 0L ) /* check for beginning of file */
- { fseek(fp, -1L , CURRENT_REL ) ; /* back up one char */
- c = getc(fp) ; /* read a char */
- /* back up in front of char read */
- fseek(fp, -1L, CURRENT_REL ) ;
- return( toascii(c) ) ; /* force it to ASCII */
- } /* and return it */
- else return(EOF_MARK) ;
- }
-
- int close_file()
- {
- fclose(fp) ;
- }
-
-
- int read_line(buf,nread) /* read one line of characters */
- char buf[] ; /* put the data here */
- int nread ; /* maximum number of bytes to read */
- {
- int i , c ;
-
- i = 0 ;
- for( i=1 ; i < nread ; i= i+1 )
- {
- c = getc(fp) ;
- if( c == EOF )
- break ;
- buf[i] = c ;
- }
- return( i ) ; /* return number of chars read */
- }
-
-
-